package org.moparscape.msc.gs.core;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.moparscape.msc.config.Config;
import org.moparscape.msc.gs.model.World;


public class TaskManager {

    private ScheduledExecutorService eService = Executors.newScheduledThreadPool(Config.SERVER_THREAD_COUNT);

    public void assignProcessor(Runnable r) {
        eService.submit(r);
    }

    public void scheduledProcessor(Runnable r, int time) {
        eService.schedule(r, time, TimeUnit.MILLISECONDS);
    }

    public void checkAndBlockThread() {
        boolean going = false;
        if(World.getWorld().isUpdating()) {
            while(World.getWorld().isUpdating()) {
                if(!going) {
                    System.out.println("Waiting for update to finish! " + Thread.currentThread().getName());
                    going = true;
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(going)
                System.out.println("Update Finished " + Thread.currentThread().getName());
        }
    }

}